import COS_SDK from 'cos-js-sdk-v5'

// COS配置
const config = {
    name: 'sls-cloudfunction-ap-guangzhou-code-1309209904',
    area: 'ap-guangzhou',
    id: 'AKID3wbz6xXqsd8NL2TeX9VaOvBBuLVP3QWM',
    key: 'atue8MK0QZR2NREkyvb07yYsvhDRP5SZ'
}

// 创建客户端
const client = new COS_SDK({
    SecretId: config.id,
    SecretKey: config.key,
})

const cos = {
    // 上传
    up (params:any,name:any){
        return new Promise((resolve, reject) => {
            client.putObject({
                Bucket: config.name,
                Region: config.area,
                Key: name,
                StorageClass: 'STANDARD',
                Body: params,
                onProgress: params.progress
            }, function(err, data: any) {
                if(err) reject(err);
                else resolve(data);
            });
        })
    },
    // 获取带签名的URL
    getUrl (name:any){
        return new Promise((resolve, reject) => {
            client.getObjectUrl({
                Bucket: config.name,
                Region: config.area,
                Key: name
            }, function(err, data){
                if(err) reject(err);
                else resolve({location: data.Url});
            })
        })
    },
}

export default cos